<template>
  <div>
    <h2 v-if="flag">添加:</h2>
    <h2 v-else>编辑:{{ label }}</h2>
    <!-- 写一个input输入框左边标签名称为标题 -->
    <label>标题：</label>
    <input type="text" v-model="label" />
    <!-- 写一个input输入框左边标签名称为日期 -->
    <label>日期：</label>
    <input type="date" v-model="date" />
    <!-- 写一个提交和添加按钮，两个按钮间距15px-->
    <button class="submit" @click="submit">提交</button>   
    <button class="add" @click="add">添加</button>
    <!-- 写一个表格，表格中有标题、日期、操作三列，三列间距15px 还有边款线，操作列中有编辑和删除两个按钮 -->
    <table>
      <thead>
        <tr>
          <th>标题</th>
          <th>日期</th>
          <th>操作</th>
        </tr>
      </thead>
      <div class="display" @scroll="handleScroll">
        <div class="scroll">
          <div class="placeholder" :style="{height:currentIndex * 40 + 'px'}"></div>
          <div class="content">
                <tbody>
                  <tr v-for="item in currentList" :key="item.id" >
                    <td>{{ item.label}}</td>
                    <td>{{ item.date }}</td>
                    <td>
                      <button @click="edit(item.id)">编辑</button>
                      <button @click="del(item.id)">删除</button>
                    </td>
                  </tr>
                </tbody>
          </div>
        </div>
     </div>
    </table>
  </div>
</template>

<script>
export default {
  name: 'children',
  components: {},
  data() {
    return {
        flag: true,
        label: '',
        date: '',
        id: undefined,
        list: JSON.parse(localStorage.getItem('list')) || [],
        currentIndex: 0
    }
  },
  computed: {
    currentList() {
      return this.list.slice(0,this.currentIndex+5)
    }
  },

  mounted() {},
  methods: {
    add() {
      if (!this.flag) return alert('编辑时不可添加') 
      if (!this.label || !this.date) return alert('请输入标题和日期')
      this.list.push({
        id: Date.now(),
        label: this.label,
        date: this.date
      })
      localStorage.setItem('list', JSON.stringify(this.list))
      this.label = ''
      this.date = '' 
    },
    del(id){
      this.list = this.list.filter(item => item.id !== id)
      localStorage.setItem('list', JSON.stringify(this.list))
    },
    edit(id){
      this.flag = false
      const item = this.list.find(item => item.id === id)
      this.label = item.label
      this.date = item.date
      this.id = id
    },
    submit(){
      if (this.flag) return alert('添加时不可提交')
      if (!this.label || !this.date) return alert('请输入标题和日期')
      const item = this.list.find(item => item.id === this.id)
      item.label = this.label
      item.date = this.date
      localStorage.setItem('list', JSON.stringify(this.list))
      this.label = ''
      this.date = ''
      this.flag = true
    },
    handleScroll(e){
      // currentIndex为当前滚动条滚动的距离
      this.currentIndex = e.target.scrollTop/40
      this.currentIndex = Math.floor(this.currentIndex)

    }

  }
}
</script>

<style>
/*表头使用flex布局，justify-content: space-between; 使标题、日期、操作三列间距15px*/
thead tr {
  display: flex;
  justify-content: space-between;
  width: 240px;
}
/*表格中的内容使用flex布局，justify-content: space-between; 使标题、日期、操作三列间距15px*/
tbody tr {
  display: flex;
  justify-content: space-between;
  width: 280px;
}
tr{
  height: 40px!important;
  line-height: 40px!important;
}
/*给表格的内容添加黑色粗的边框线*/
.scroll {
  border: 1px solid black;
  height: 600px;
}
/*display的高度为400px，overflow: auto;使表格的内容超出400px时出现滚动条*/
.display {
  height: 200px;
  overflow: auto;
}











</style>
